//package net.xmmpp.uc.im.bootstrap.web.config;
//
//
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
//import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers;
//import org.springframework.boot.autoconfigure.cache.CacheProperties;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//
//import java.util.LinkedHashSet;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@EnableCaching
//@Configuration
//@EnableConfigurationProperties(CacheProperties.class)
//public class CacheConfig {
//
//    @Bean
//    @ConditionalOnMissingBean
//    public CacheManagerCustomizers cacheManagerCustomizers(ObjectProvider<CacheManagerCustomizer<?>> customizers) {
//        return new CacheManagerCustomizers(customizers.orderedStream().collect(Collectors.toList()));
//    }
//
//    @Primary
//    @Bean
//    RedisCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers cacheManagerCustomizers,
//                                   ObjectProvider<RedisCacheConfiguration> redisCacheConfiguration,
//                                   RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader,
//                                   Jackson2JsonRedisSerializer valueSerializationPair) {
//        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(
//                determineConfiguration(cacheProperties, redisCacheConfiguration, resourceLoader.getClassLoader(), valueSerializationPair));
//
//        List<String> cacheNames = cacheProperties.getCacheNames();
//        if (!cacheNames.isEmpty()) {
//            builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
//        }
//
//        return cacheManagerCustomizers.customize(builder.build());
//    }
//
//    private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
//            CacheProperties cacheProperties,
//            ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
//            ClassLoader classLoader,
//            Jackson2JsonRedisSerializer valueSerializationPair) {
//        return redisCacheConfiguration.getIfAvailable(() -> createConfiguration(cacheProperties, classLoader, valueSerializationPair));
//    }
//
//    private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(
//            CacheProperties cacheProperties, ClassLoader classLoader,
//            Jackson2JsonRedisSerializer valueSerializationPair) {
//
//        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
//        org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
//                .defaultCacheConfig();
//
//        config = config.serializeValuesWith(
//                RedisSerializationContext.SerializationPair.fromSerializer(valueSerializationPair));
//
//        if (redisProperties.getTimeToLive() != null) {
//            config = config.entryTtl(redisProperties.getTimeToLive());
//        }
//        if (redisProperties.getKeyPrefix() != null) {
//            config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
//        }
//        if (!redisProperties.isCacheNullValues()) {
//            config = config.disableCachingNullValues();
//        }
//        if (!redisProperties.isUseKeyPrefix()) {
//            config = config.disableKeyPrefix();
//        }
//
//        return config;
//    }
//}
